package TestResult;

import jiazhong.pojo.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import tools.initLogRecord;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


//独立写sql
public class Test1 {


    @Test
    public void findAll() throws IOException {

        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        List<Emp> emps = sqlSession.selectList("EmpMapper.findAll");
        for (Emp obj : emps) {
            System.out.println("obj = " + obj);
        }
    }

    @Test
    public void insert() throws IOException {
        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        int i = sqlSession.update("EmpMapper.insert");
        sqlSession.commit();//提交事务
        System.out.println("i = " + i);
    }

    @Test
    public void update() throws IOException {
        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        int i = sqlSession.update("EmpMapper.update");
        sqlSession.commit();//提交事务
        System.out.println("i = " + i);
    }

    @Test
    public void delete() throws IOException {
        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        int i = sqlSession.update("EmpMapper.delete");
        sqlSession.commit();//提交事务
        System.out.println("i = " + i);
    }

    //根据参数来查询
    @Test
    public void findone() throws IOException {
        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        Object o = sqlSession.selectOne("EmpMapper.findone", 5);

        System.out.println("o = " + o);

    }

    //根据参数来增加
    @Test
    public void insert1() throws IOException {

        HashMap hashMap = new HashMap();
        hashMap.put("name", "22");
        hashMap.put("tel", "123456");
        hashMap.put("dress", "444");
        hashMap.put("money", 55);
        hashMap.put("sta", "0");

        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        int i = sqlSession.update("EmpMapper.insert1", hashMap);
        sqlSession.commit();//提交事务
        System.out.println("i = " + i);
    }

    //根据参数来修改
    @Test
    public void update1() throws IOException {
        HashMap hashMap = new HashMap();
        hashMap.put("name", "22");
        hashMap.put("tel", "123456");
        hashMap.put("dress", "444");
        hashMap.put("money", 55);
        hashMap.put("sta", "0");
        hashMap.put("empId", 12);

        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        int i = sqlSession.update("EmpMapper.update1", hashMap);
        sqlSession.commit();//提交事务
        System.out.println("i = " + i);
    }

    //根据参数来删除
    @Test
    public void delete1() throws IOException {
        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        int i = sqlSession.update("EmpMapper.delete1", 12);
        sqlSession.commit();//提交事务
        System.out.println("i = " + i);
    }

    //字段查询
    @Test
    public void findoneZIduan() throws IOException {
        //日志管理
        initLogRecord.initLog();
        HashMap hashMap = new HashMap();
        hashMap.put("cols", "name,tel");
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        List<Emp> emps = sqlSession.selectList("EmpMapper.findoneziduan", hashMap);
        for (Emp obj : emps) {
            System.out.println("obj = " + obj);
        }
    }

    //模糊查询
    @Test
    public void findone2() throws IOException {
        HashMap hashMap = new HashMap();
        hashMap.put("name", "1");
        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        List<Emp> emps = sqlSession.selectList("EmpMapper.findone2", hashMap);
        for (Emp obj : emps) {
            System.out.println("obj = " + obj);
        }
    }

    //限制查询
    @Test
    public void findone3() throws IOException {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("minmoney", 1);
        stringObjectHashMap.put("maxmoney", 60);
        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        List<Emp> emps = sqlSession.selectList("EmpMapper.findone3", stringObjectHashMap);
        for (Emp obj : emps) {
            System.out.println("obj = " + obj);
        }
    }

    //批量来删除
    @Test
    public void delete2() throws IOException {
        Integer[] ids = {4};
        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        int i = sqlSession.delete("EmpMapper.delete2", ids);
        sqlSession.commit();//提交事务
        System.out.println("i = " + i);
    }

    //批量更新
    @Test
    public void update2() throws IOException {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        Integer money = 100;
        Integer[] ids = {3, 5, 7};
        stringObjectHashMap.put("arrIds", ids);
        stringObjectHashMap.put("money", money);
        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        int i = sqlSession.update("EmpMapper.update2", stringObjectHashMap);
        sqlSession.commit();//提交事务
        System.out.println("i = " + i);
    }

    //返回map查询
    @Test
    public void findAll1() throws IOException {
        //日志管理
        initLogRecord.initLog();
        //1.读取加载核心配置文件
        InputStream in = Resources.getResourceAsStream("config/mybatis-config.xml");
        //2.构造会话工厂
        SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
        //3.创建对象会话
        SqlSession sqlSession = fac.openSession();
        //4.执行器
        Map<Integer, Object> map = sqlSession.selectMap("EmpMapper.getresult", "empId");
        Set<Integer> integers = map.keySet();
        for (Integer obj : integers) {
            System.out.println("obj = " + obj);
            System.out.println("map.get(obj) = " + map.get(obj));
        }
    }


}
